<?php

namespace app\admin\controller\store;

use app\common\controller\Backend;
use think\Db;
use think\Session;

/**
 * 库存
 *
 * @icon fa fa-circle-o
 */
class Stock extends Backend
{
    
    /**
     * Stock模型对象
     * @var \app\admin\model\Stock
     */
    protected $model = null;
    public $type=null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Stock;
        $type = $this->request->request("type");
        if ($type==null)$type=1;
        $this->type=$type;
        $this->assign('type',$type);
        $this->view->assign("typeList", $this->model::getTypeList());

    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {

//        $admin = Session::get('admin');
        $userid = $this->auth->userid;
        $map = array();

        if ($userid==1){
            $db = new \app\admin\model\Githouse();
            $this->assign("data",$db->select());
        }
        //$this->error("暂无权限");
        $this->assign("userid",$userid);
        $gitDb = new \app\admin\model\Githouse();
        //获取仓库的权限
        if ($userid==1){
            $cids = 0;
        }else{
            $gitInfo = $gitDb->where(array('manger'=>$userid))->find();
            $cids = $gitInfo['id'];
            if (!$cids)$this->error("暂无权限");
        }
        $this->assign("cids",$cids);

        /*半成品
         * 当前用户是超级用户还是总部管理员
         *
         * */
        $this->request->filter(['strip_tags']);
        if ($this->type==1){

            if ($this->request->isAjax()) {
                //如果发送的来源是Selectpage，则转发到Selectpage
                if ($this->request->request('keyField')) {
                    return $this->selectpage();
                }
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $whe = array();
                $filter=json_decode($this->request->get("filter"),1);
                if ($filter && isset($filter['cid'])) {

                    $whe['cid'] =$filter['cid'];
                }
                if ($cids > 1){
                    $whe['cid']=$cids;
                }
                if (isset($filter['cname']) && $filter['cname']!=''){
                    $whe['shopid']=array("in",$filter['cname']);
                }
                $total = $this->model
                    ->where($whe)
                    ->order($sort, $order)
                    ->count();

                $list = $this->model
                    ->where($whe)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

                $list = collection($list)->toArray();
                $result = array("total" => $total, "rows" => $list);

                return json($result);
            }
            return $this->view->fetch();

        }else if ($this->type==2){
            //成品搜索
            if ($this->request->isAjax()) {
                //如果发送的来源是Selectpage，则转发到Selectpage
                if ($this->request->request('keyField')) {
                    return $this->selectpage();
                }
                $filter=json_decode($this->request->get("filter"),1);
                if ($filter && isset($filter['cid'])) {
                   $cid =$filter['cid'];
                }
                if ($cids > 1){
                   $cid=$cids;
                }
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                if (isset($filter['cname']) && intval($filter['cname']) > 0){
                        $shopid = intval($filter['cname']);
                        $pDb = new \app\admin\model\Preproduct();
                        $shop = $pDb->where(array('id'=>$shopid))->find();
                        $bcp = $shop['bcp'];
                        if (empty($bcp))return json( array("total" => 0, "rows" => []));


                        $total = $pDb ->where("id in ($bcp)")->order($sort, $order) ->count();

                        $list = $pDb ->where("id in ($bcp)")
                            ->order($sort, $order)
                            ->limit($offset, $limit)
                            ->select();

                     $arr = collection($list)->toArray();
                        $data=array();
                        $min=array();//最小值
                        foreach ($arr as $k=>$v){
                            $sto = $this->model->where(array("cid"=>$cid,"shopid"=>$v['id']))->find();
                            if ($sto){
                                $num = $sto['num'];
                            }else{
                                $num = 0;
                            }
                            $data[]=array(
                                "id"=>$v['id'],
                                "shopid"=>$v['cname'],
                                "num"=>$num

                            );
                            $min[]=$num;
                        }
                        $sto_min = min($min);
                        $str="此产品可以组成成品  ".$sto_min."  套";
                        $data[]=array(
                            'id'=>$str,
                            "shopid"=>'',
                            "num"=>''
                        );
                    $result = array("total" => $total, "rows" => $data);

                    return json($result);


                }else{
                    return json( array("total" => 0, "rows" => []));
                }


            }
            return $this->view->fetch("index2");

        }else{
            if ($this->request->isAjax()) {
                //如果发送的来源是Selectpage，则转发到Selectpage
                if ($this->request->request('keyField')) {
                    return $this->selectpage();
                }
                list($where, $sort, $order, $offset, $limit) = $this->buildparams();
                $whe = array();
                $filter=json_decode($this->request->get("filter"),1);


                if (isset($filter['cname']) && $filter['cname']!=''){
                    $whe['shopid']=array("in",$filter['cname']);
                }

                $sql="";
                if ($whe){
                    $sql="select shopid,sum(num) as num from hdyj_stock where shopid in({$filter['cname']})  group by shopid order by shopid limit $offset,$limit";
                }else{
                    $sql="select shopid,sum(num) as num from hdyj_stock  group by shopid order by shopid limit $offset,$limit";

                }
                $pdb=new \app\admin\model\Preproduct();

                $list = $this->model->query($sql);
                $total = $this->model->group('shopid')->where($whe)->count();
                $list = collection($list)->toArray();
                foreach ($list as $k=>$v){
                    $list[$k]['id']=$v['shopid'];
                    $pinfo = $pdb->field('cname')->find($v['shopid']);

                    $list[$k]['shopid']=$pinfo['cname'];

                }
                $result = array("total" => $total, "rows" => $list);

                return json($result);
            }

            return $this->view->fetch("index3");
        }



    }




}
